#include "dia_addstudent.h"
#include "ui_dia_addstudent.h"
#include<QMessageBox>
Dia_AddStudent::Dia_AddStudent(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dia_AddStudent)
{
    ui->setupUi(this);
}

Dia_AddStudent::~Dia_AddStudent()
{
    delete ui;
}

void Dia_AddStudent::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/img/east.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}

void Dia_AddStudent::setType(bool isAdd,StuInfo info)
{
    m_isAdd=isAdd;
    m_info=info;
    //更新时回显数据
    ui->le_name->setText(info.name);
    ui->sb_age->setValue(info.age);
    ui->le_grade->setText(QString::number(info.grade));
    ui->le_class->setText(QString::number(info.uiclass));
    ui->le_seq->setText(QString::number(info.studentid));
    ui->le_phone->setText(info.phone);
    ui->le_wechat->setText(info.wechat);
}

//取消
void Dia_AddStudent::on_btn_cancel_clicked()
{
   this->hide();
}

//保存
void Dia_AddStudent::on_btn_save_clicked()
{
    StuInfo info;
    auto ptr=stuSql::getinstance();
    info.name=ui->le_name->text();
    info.age=ui->sb_age->text().toInt();
    info.grade=ui->le_grade->text().toInt();
    info.uiclass=ui->le_class->text().toInt();
    info.studentid=ui->le_seq->text().toInt();
    info.phone=ui->le_phone->text();
    info.wechat=ui->le_wechat->text();
    if(m_isAdd)
        //添加
        ptr->addStu(info);
    else{
        //更新
        info.id=m_info.id;
        ptr->updateStuInfo(info);
    }
    QMessageBox::information(nullptr,"信息","保存成功");
    this->hide();
}
